package com.reggie.lotterysystem.controller;

import com.reggie.lotterysystem.common.pojo.CommonResult;
import com.reggie.lotterysystem.common.utils.JacksonUtil;
import com.reggie.lotterysystem.controller.param.DrawPrizeParam;
import com.reggie.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.reggie.lotterysystem.controller.result.WinningRecordResult;
import com.reggie.lotterysystem.dao.dataobject.WinningRecordDO;
import com.reggie.lotterysystem.service.DrawPrizeService;
import com.reggie.lotterysystem.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {

     private final static Logger logger = LoggerFactory.getLogger(DrawPrizeController.class);

     @Autowired
     private DrawPrizeService drawPrizeService;

     @RequestMapping("/draw-prize")
     public CommonResult<Boolean> drawPrize(
             @Validated @RequestBody DrawPrizeParam param){
          logger.info("drawPrize DrawPrizeParam:{}",
                  JacksonUtil.writeValueAsString(param));

          //service
          drawPrizeService.drawPrize(param);
          return CommonResult.success(true);
     }

     @RequestMapping("/winning-records/show")
     public CommonResult<List<WinningRecordResult>> showWinningRecords(
             @Validated @RequestBody ShowWinningRecordsParam param) {
          logger.info("showWinningRecords ShowWinningRecordsParam:{}",
                  JacksonUtil.writeValueAsString(param));
          List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
          return CommonResult.success(
                  convertToWinningResultList(winningRecordDTOList));
     }

     private List<WinningRecordResult> convertToWinningResultList(
             List<WinningRecordDTO> winningRecordDTOList) {
          if (CollectionUtils.isEmpty(winningRecordDTOList)){
               return Arrays.asList();
          }
          return winningRecordDTOList.stream()
                  .map(winningRecordDTO ->{
                       WinningRecordResult winningRecordResult = new WinningRecordResult();
                       winningRecordResult.setWinnerId(winningRecordDTO.getWinnerId());
                       winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
                       winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
                       winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
                       winningRecordResult.setPrizeTires(winningRecordDTO.getPrizeTier().getMessage());
                       return winningRecordResult;
                  }).collect(Collectors.toList());
     }


}
